//
//  DesDataHandle.m
//  穷游
//
//  Created by lanou3g on 15/12/18.
//  Copyright © 2015年 王忠奎. All rights reserved.
//

#import "DesDataHandle.h"
#import "DesModel.h"
static DesDataHandle *_handle = nil;
@implementation DesDataHandle

+(instancetype)sharedDesDataHandle
{
    
    return [[self alloc]init];
    
    
}

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    
    static dispatch_once_t onecToken;
    dispatch_once(&onecToken, ^{
        if (_handle == nil) {
            _handle = [super allocWithZone:zone];
        }
    });
    return _handle;
    
}



#pragma mark ---请求数据
-(void)getDesDataFromNet:(BLOCK)finishBlock
{

    //网址字符串
    NSString *string = @"http://open.qyer.com/qyer/footprint/continent_list?app_installtime=1435837675&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=31.13012042476571&lon=121.2840234213879&page=1&track_app_channel=App%2520Store&track_app_version=6.3&track_device_info=iPhone7%2C2&track_deviceid=94600906-5BB7-43E7-9E16-C479A03EE469&track_os=ios%25208.3&v=1";
    
    //包装为url
    NSURL *url = [NSURL URLWithString:string];
    //创建网络请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //利用NSURLSession发送网络请求
    NSURLSession *session = [NSURLSession sharedSession];
    //发送
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        @autoreleasepool {
            if (data == nil || error != nil) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    finishBlock(nil ,nil);
                    return;
                });
            }
            
            self.desDataDict = [NSMutableDictionary dictionary];
            //解析数据
            NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            //打印验证
            //NSLog(@"%@",resultDict);
            
            //从大字典中找出需要的字段data,为一个数组
            NSArray *dataArray = resultDict[@"data"];
            
//            初始化临时数组
            NSMutableArray *array = [NSMutableArray array];

            //遍历data字段
            for (NSDictionary *dict in dataArray) {
                
                [array addObject:[dict valueForKey:@"cnname"]];
                
//                从小字典里取出country字段
                NSArray *dataArr = [dict valueForKey:@"country"];
//                初始化临时数组
                NSMutableArray *arr = [NSMutableArray array];
                for (NSDictionary *d in dataArr) {
                    //初始化model
                    DesModel *model = [[DesModel alloc]init];
//                    [model setValuesForKeysWithDictionary:d];
                    model.caname = [d valueForKey:@"cnname"];
                    model.count = [d valueForKey:@"count"];
                    model.enname = [d valueForKey:@"enname"];
                    model.label = [d valueForKey:@"label"];
                    model.photo = [d valueForKey:@"photo"];
                  
                    [arr addObject:model];
                    NSLog(@"////////%@",model.caname);
                }
                //[self.desDataArray addObject:arr];
               
                //大字典key是大洲名,value是国家数组
                [self.desDataDict setValue:arr forKey:[dict valueForKey:@"cnname"]];
                
//                [array addObject:[dict valueForKey:@"cnname"]];
            
            }
            
            
            
            
            
            
            NSLog(@"\\\\\\\%@",self.desDataDict);
            self.desZhouArray = [NSArray arrayWithArray:array];
//            NSLog(@"%@",self.desZhouArray);
            
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                finishBlock(self.desZhouArray,self.desDataDict);
            });
            
        }
    }];
    
    [task resume];
    
    
}

@end
